/*
 *      level.cpp
 *      
 *      2009 Víctor Manuel Ochoa <kanito8a@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#include "level.h"


Level::Level()//: Entity()
{
	
}

bool Level::init(TextureManager *_texture_manager,const char *_cfg_filename)
{

	Entity::init();
	
	if(_cfg_filename==0 || _texture_manager==0){
		return false;		
	}
	
	FileReader description_file;
	char *value;
	int current_column=1;
	
	m_pLog=&Log::getInstance();
	
	
	if(!description_file.init(_cfg_filename))
	{
		description_file.freeMemory();
		m_pLog->error("Unable to create Level"); 
		return false;
	}
	
	m_texture=_texture_manager->newTexture(description_file.getValue(STRING_TEXTURE_FILENAME,1));
	m_columns_num=atoi(description_file.getValue(STRING_COLUMNS2,1));

	if(!m_texture || m_columns_num==0)
	{
		description_file.freeMemory();
		freeMemory();
		m_pLog->error("Unable to create Level"); 
		return false;
	}

	for(int i=0;i<MAX_HEIGHT;i++){
		for(int j=0;j<MAX_WIDTH;j++){
			m_tiles[i][j]=0;
		}
	}
	
	value=description_file.getValue(STRING_COLUMNS_HEIGHT,current_column);
	
	while(value)
	{
		int current_value=atoi(value);
		for(int i=MAX_HEIGHT-1;i>=MAX_HEIGHT-current_value;i--){
			m_tiles[i][current_column-1]=1;
		}
		++current_column;
		value=description_file.getValue(STRING_COLUMNS_HEIGHT,current_column);
	}
			
	description_file.freeMemory();
	m_pLog->debug("+ Level");
	return true;
}

void Level::freeMemory()
{
	Entity::freeMemory();
	m_pLog->debug("- Level"); 
	m_pLog=0;
}
		
void Level::draw(){
	printf(",");
	for(int i=0;i<MAX_HEIGHT;i++){
		for(int j=0;j<m_columns_num;j++){
			if(m_tiles[i][j]){
				drawTile(j*TILE_SIZE,i*TILE_SIZE);
			}
		}
	}	
}

void Level::update(){
	//~ Physics::update();
	setAlive(true);
}

int Level::getDistanceToFloor(int _x,int _y)
{
	return 999;	
}

int Level::getDistanceToWall(int _x,int _y)
{
	return 999;	
}

void Level::drawTile(int _x,int _y)
{
	//~ m_texture->bind();
	
	//printf("%d,%d ",_x,_y);
	//~ glColor3fv(Entity::getColor());
	glDisable( GL_BLEND );
	glColor4f(0.f,0.f,0.f,1.f);
		
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(_x,_y, 0);
	
    glBegin(GL_QUADS);
    
      glVertex2f(0,0);  
	  glVertex2f(TILE_SIZE,0);  
	  glVertex2f(TILE_SIZE,TILE_SIZE);  
	  glVertex2f(0,TILE_SIZE);      	   
      //~ glVertex2f(10,10);  
	  //~ glVertex2f(20,10);  
	  //~ glVertex2f(20,20);  
	  //~ glVertex2f(10,20);
    glEnd();	
	glPopMatrix();

}
